"""
主题: 自定义字符串的格式化
问题: 你想通过 format() 函数和字符串方法使得一个对象能支持自定义的格式化。
提示 : 
"""

_formats = {
    'ymd': '{d.year}-{d.month}-{d.day}',
    'mdy': '{d.month}/{d.day}/{d.year}',
    'dmy': '{d.day}/{d.month}/{d.year}'
}


class Date:
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day = day

    def __format__(self, code):
        if code == '':
            code = 'ymd'
        fmt = _formats[code]
        return fmt.format(d=self)

def recipe1():
    d = Date(2012, 12, 21)
    print(d)
    print(format(d, 'mdy'))
    print('The date is {:ymd}'.format(d))
    print('The date is {:mdy}'.format(d))

def recipe2():
    from datetime import date
    d = date(2012, 12, 21)
    print(format(d))
    print(format(d,'%A, %B %d, %Y'))
    print('The end is {:%d %b %Y}. Goodbye'.format(d))

def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()



if __name__ == '__main__':
    main()            